<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '..');

require_once 'components/utils/debug_utils.php';

Warning::BeginSuppress(PHPWarning::$Deprecated);
Warning::BeginSuppress(PHPWarning::$Strict);

require_once 'components/utils/debug_utils.php';

Warning::BeginSuppress(PHPWarning::$Deprecated);
require_once 'simpletest/autorun.php';
require_once 'simpletest/mock_objects.php';
Warning::EndSuppress();

require_once 'database_engine/common_engine.php';
require_once 'database_engine/dataset.php';
require_once 'database_engine/commands.php';
require_once 'database_engine/datareader_stub.php';

Mock::generate('IConnection');
Mock::generate('IDataReader');

class DatasetTests extends UnitTestCase
{
    private $datasetMetadata;

    /**
     * @return SimpleMock|IConnection
     */
    private function CreateMockConnection()
    {
        return new MockIConnection();
    }

    public function setUp()
    {
        parent::setUp();
        $this->datasetMetadata =
                new TableMetadata('test_table',
                    array(
                        'id' => 'integer',
                        'id_2' => 'integer',
                        'value' => 'string')
                );
    }

    public function testSimpleDatasetReading()
    {
        $connection = $this->CreateMockConnection();

        /** @var SimpleMock|IDataReader $dataReader  */
        $dataReader = new MockIDataReader();
        $connection->setReturnReference('ExecuteReader',
            $dataReader,
            array('SELECT t0.id AS id, t0.id_2 AS id_2, t0.value AS value FROM test_table t0 WHERE (t0.id = 1) AND (t0.id_2 = 2)'));

        $dataset = new EditableDataset($connection, $this->datasetMetadata);
        $dataset->AddSimpleCondition('[id]', ConditionOperator::$Equals, '1');
        $dataset->AddSimpleCondition('[id_2]', ConditionOperator::$Equals, '2');
        
        $dataset->Open();

        $connection->expectCallCount('ExecuteReader', 1);

        $dataset->Close();
    }

    public function testSimpleDatasetEditing()
    {
        $connection = $this->CreateMockConnection();

        $dataReader = new EditableDataReaderStub(array(
            array(
                'id' => 1,
                'id_2' => 1,
                'value' => 'str value'
            )
        ), array('id'));
        $dataReader->Open();

        $connection->setReturnReference('ExecuteReader',
            $dataReader,
            array('SELECT t0.id AS id, t0.id_2 AS id_2, t0.value AS value FROM test_table t0 WHERE (t0.id = 1) AND (t0.id_2 = 2)'));

        $dataset = new EditableDataset($connection, $this->datasetMetadata);
        $dataset->AddSimpleCondition('[id]', ConditionOperator::$Equals, '1');
        $dataset->AddSimpleCondition('[id_2]', ConditionOperator::$Equals, '2');

        $dataset->Open();

        $dataset->Next();
        
        $dataset->Edit();

        $dataset->SetFieldValueByName('[id]', 2);
        $dataset->SetFieldValueByName('[id_2]', 2);
        $dataset->SetFieldValueByName('[id]', 'value');

        $dataset->Post();

//        $connection->expectOnce(
//            'ExecuteNoResult',
//            array('UPDATE test_table SET id = 2, id_2 = 2, value = \'value\' WHERE id = 1'));

        $dataset->Close();
         
    }
}


?>